<template>
  <div>

      <we-button v-if="baseCode" @click="dialogFormVisible = true" type="primary" size="mini">
        <em class="el-icon-plus"></em> 新增
      </we-button>
      <we-button v-if="baseCode" @click="detailRow(currentRow)" type="primary" size="mini">
        <em class="el-icon-edit"></em> 修改
      </we-button>
      <we-button v-if="baseCode" @click="deleteRow"  type="primary" size="mini">
        <em class="el-icon-delete"></em> 删除
      </we-button>
    <span v-if="baseName">当前人员：【 {{baseName}} 】</span>


    <!-- 表格 -->
    <el-table
      size="mini"
      height="116px"
      :header-cell-style="rowClass"
      :data="tableData"

      border
      highlight-current-row
      resizable
      ref="singleTable"
      @row-dblclick="detailRow"
      @current-change="handleCurrentChange"
      style="width: 100%; margin-top: 5px"
    >
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column
        prop="nameUsedBefore"
        :show-overflow-tooltip="true"
        width="150"
        label="曾用名"
      >
      </el-table-column>
      <el-table-column
        prop="dutySituation_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="在岗/内退"
      >
      </el-table-column>
      <el-table-column
        prop="interruptedWorkingYears"
        :show-overflow-tooltip="true"
        width="150"
        label="间断工龄"
      >
      </el-table-column>
      <el-table-column
        prop="continuousWorkingYears"
        :show-overflow-tooltip="true"
        width="150"
        label="连续工龄"
      >
      </el-table-column>
      <el-table-column
        prop="enterSystemTime"
        :show-overflow-tooltip="true"
        width="150"
        label="进本系统时间"
      >
      </el-table-column>
      <el-table-column
        prop="sort"
        :show-overflow-tooltip="true"
        width="150"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        prop="professionalCategory_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="从事专业类别"
      >
      </el-table-column>
      <el-table-column
        prop="professionalDescription"
        :show-overflow-tooltip="true"
        width="150"
        label="从事专业描述"
      >
      </el-table-column>
      <el-table-column
        prop="addManDate"
        :show-overflow-tooltip="true"
        width="150"
        label="增员日期"
      >
      </el-table-column>
      <el-table-column
        prop="addManWays_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="增员途径"
      >
      </el-table-column>
      <el-table-column
        prop="employmentForm_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="用工形式"
      >
      </el-table-column>
      <el-table-column
        prop="employmentDuration_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="用工期限"
      >
      </el-table-column>
      <el-table-column
        prop="reductionManDate"
        :show-overflow-tooltip="true"
        width="150"
        label="减员日期"
      >
      </el-table-column>
      <el-table-column
        prop="reductionManWays_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="减员途径"
      >
      </el-table-column>
      <el-table-column
        prop="archivesLocation"
        :show-overflow-tooltip="true"
        width="150"
        label="档案所在地"
      >
      </el-table-column>
      <el-table-column
        prop="homeAddress"
        :show-overflow-tooltip="true"
        width="150"
        label="家庭住址"
      >
      </el-table-column>
      <el-table-column
        prop="registeredResidenceNature_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="户口性质"
      >
      </el-table-column>
      <el-table-column
        prop="registeredResidenceLocation"
        :show-overflow-tooltip="true"
        width="150"
        label="户口所在地"
      >
      </el-table-column>
       <el-table-column
        prop="homeTelephone"
        :show-overflow-tooltip="true"
        width="150"
        label="住宅电话"
      >
      </el-table-column>
      <el-table-column
        prop="email"
        :show-overflow-tooltip="true"
        width="150"
        label="电子邮箱"
      >
      </el-table-column>
      <el-table-column
        prop="workTelephone"
        :show-overflow-tooltip="true"
        width="150"
        label="办公电话"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        :show-overflow-tooltip="true"
        width="150"
        label="手机号码"
      >
      </el-table-column>
      <el-table-column
        prop="fax"
        :show-overflow-tooltip="true"
        width="150"
        label="传真"
      >
      </el-table-column>
      <el-table-column
        prop="isValid"
        :show-overflow-tooltip="true"
        width="150"
        label="是否有效"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        :show-overflow-tooltip="true"
        width="150"
        label="备注"
      >
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <we-button @click="detailRow(scope.row)" type="primary" size="mini"
            >详情</we-button
          >
          <we-button type="danger" @click="deleteRow(scope.row)" size="mini">删除</we-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="table_tools">
      <we-pagination
        :total="pagination.total"
        :page="pagination.pageNum"
        :pageSize="pagination.pageSize"
        @currentChange="currentChange($event)"></we-pagination>
    </div>
    <!-- 表格 -->

    <!-- 新增/编辑弹窗 -->
    <el-dialog
      title="人员基础信息子集"
      :visible.sync="dialogFormVisible"
      @close="handleOffDialog"
      top="60px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
        size="mini"
        style="height: 500px;overflow-y: auto;"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="曾用名" prop="nameUsedBefore">
              <el-input
                  class="inputW"
                  v-model="ruleForm.nameUsedBefore"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="在岗/内退" prop="dutySituation">
              <el-select
                  class="inputW"
                  v-model="ruleForm.dutySituation"
                  placeholder="请选择"
                  clearable
              >
                <el-option v-for="item in anJobConditionList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="间断工龄" prop="interruptedWorkingYears">
              <el-input
                  class="inputW"
                  v-model="ruleForm.interruptedWorkingYears"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="连续工龄" prop="continuousWorkingYears">
              <el-input
                  class="inputW"
                  v-model="ruleForm.continuousWorkingYears"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进本系统时间" prop="enterSystemTime">
              <el-date-picker
                  v-model="ruleForm.enterSystemTime"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input
                  class="inputW"
                  v-model="ruleForm.sort"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="从事专业类别" prop="professionalCategory">
              <el-cascader
                  class="inputW"
                  v-model="ruleForm.professionalCategory"
                  :options="subjectList"
                  :props="{ expandTrigger: 'hover' ,value:'uuid',label:'name',children: 'children'}"
                  @change="handleChange"
                  clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="从事专业描述" prop="professionalDescription">
              <el-input
                  class="inputW"
                  v-model="ruleForm.professionalDescription"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="增员日期" prop="addManDate">
              <el-date-picker
                  v-model="ruleForm.addManDate"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="增员途径" prop="addManWays">
              <el-cascader
                  class="inputW"
                  v-model="ruleForm.addManWays"
                  :options="waysList"
                  :props="{ expandTrigger: 'hover' ,value:'uuid',label:'name',children: 'children'}"
                  @change="handleChange"
                  clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用工形式" prop="employmentForm">
              <el-select
                  class="inputW"
                  v-model="ruleForm.employmentForm"
                  placeholder="请选择"
                  clearable
              >
                <el-option v-for="item in employmentFormList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用工期限" prop="employmentDuration">
              <el-select
                  class="inputW"
                  v-model="ruleForm.employmentDuration"
                  placeholder="请选择"
                  clearable
              >
                <el-option v-for="item in employmentDurationList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="减员日期" prop="reductionManDate">
              <el-date-picker
                  v-model="ruleForm.reductionManDate"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="减员途径" prop="reductionManWays">
              <el-select
                  class="inputW"
                  v-model="ruleForm.reductionManWays"
                  placeholder="请选择"
                  clearable
              >
                <el-option v-for="item in reductionManWaysList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="档案所在地" prop="archivesLocation">
              <el-input
                  class="inputW"
                  v-model="ruleForm.archivesLocation"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="家庭住址" prop="homeAddress">
              <el-input
                  class="inputW"
                  v-model="ruleForm.homeAddress"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户口性质" prop="registeredResidenceNature">
              <el-select
                  class="inputW"
                  v-model="ruleForm.registeredResidenceNature"
                  placeholder="请选择"
                  clearable
              >
                <el-option v-for="item in registerResidenceNatureList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户口所在地" prop="registeredResidenceLocation">
              <el-input
                  class="inputW"
                  v-model="ruleForm.registeredResidenceLocation"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住宅电话" prop="homeTelephone">
              <el-input
                  class="inputW"
                  v-model="ruleForm.homeTelephone"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱" prop="email">
              <el-input
                  class="inputW"
                  v-model="ruleForm.email"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="办公电话" prop="workTelephone">
              <el-input
                  class="inputW"
                  v-model="ruleForm.workTelephone"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="mobile">
              <el-input
                  class="inputW"
                  v-model="ruleForm.mobile"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传真" prop="fax">
              <el-input
                  class="inputW"
                  v-model="ruleForm.fax"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                  class="inputW"
                  v-model="ruleForm.remark"
                  autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <we-button size="mini" @click="handleOffDialog"
          >取 消</we-button
        >
        <we-button v-if="!isUpdate" size="mini" type="primary" @click="submitForm('ruleForm')"
          >新 增</we-button
        >
        <we-button v-if="isUpdate" size="mini" type="primary" @click="updateForm('ruleForm')"
          >修 改</we-button
        >
      </div>
    </el-dialog>
    <!-- 新增/编辑弹窗 -->
  </div>
</template>

<script>
import WeButton from '@/components/WeButton'
import WePagination from '@/components/WePagination'
import {bus} from "@/utils/eventBus";
import { parseTree } from '@/utils/util'
import { getAction, postAction, putAction, deleteAction } from '@/api/manage'
import {opDictNew} from "@/api/apiUrl";
export default {
  components: {
    WeButton,
    WePagination
  },
  data () {
    return {
      isUpdate: false,
      baseCode: '',
      baseName: '',
      pagination: {
        total: 10, // 总数据条数
        pageSize: 10, // 每页条数
        pageNum: 1 // 当前页
      }, // 列表分页数据
      tableData: [],
      dialogFormVisible: false,
      currentRow: null,
      ruleForm: {
        nameUsedBefore: '', // 曾用名
        dutySituation: '', // 在岗/内退
        interruptedWorkingYears: '', // 间断工龄
        continuousWorkingYears: '', // 连续工龄
        enterSystemTime: '', // 进本系统时间
        sort: '', // 排序
        professionalCategory: '', // 从事专业类别
        professionalDescription: '', // 从事专业描述
        addManDate: '', // 增员日期
        addManWays: '', // 增员途径
        employmentForm: '', // 用工形式
        employmentDuration: '', // 用工期限
        reductionManDate: '', // 减员日期
        reductionManWays: '', // 减员途径
        archivesLocation: '', // 增员途径
        homeAddress: '', // 档案所在地
        registeredResidenceNature: '', // 户口性质
        registeredResidenceLocation: '', // 户口所在地
        homeTelephone: '', // 户口所在地
        email: '', // 电子邮箱
        workTelephone: '', // 办公电话
        mobile: '', // 手机号码
        fax: '', // 传真
        isValid: '', // 是否有效，0=无效 1=有效
        remark: '' ,// 备注
        uuidList:[]
      },
      rules: {
        technicalLevelWorkersName: [
          { required: true, message: '请选择工人技术等级', trigger: 'change' }
        ],
        obtainTime: [
          {
            required: true,
            message: '请选择取得技术等级时间',
            trigger: 'change'
          }
        ],
        technicalTitle: [
          {
            required: true,
            message: '请选择聘任工人技术职务名称',
            trigger: 'change'
          }
        ],
        startTime: [
          { required: true, message: '请选择聘任起始时间', trigger: 'change' }
        ],
        typeWork: [{ required: true, message: '请选择工种', trigger: 'change' }],
        certificationTime: [
          { required: true, message: '请选择获证时间', trigger: 'change' }
        ]
      },

      anJobConditionList: [], // 在岗/内退   字典
      employmentFormList: [], // 用工形式    字典
      employmentDurationList: [], // 用工期限   字典
      reductionManWaysList: [], // 减员途径   字典
      registerResidenceNatureList: [], // 户口性质   字典
      subjectList: [], // 从事专业类别   tree
      waysList: [] // 增员途径  tree
    }
  },
  watch: {
    '$store.state.baseCode'(newval) {
      this.baseCode  = newval
      this.getList()
    },
    '$store.state.baseName'(newval) {
      this.baseName  = newval
    }
  },
  created () {
    this.baseCode  = this.$store.state.baseCode
    this.getSubjectList() // 获取专业类别表
    this.getWaysList() // 获取增员途径表
    this.getJobCondition() // 获取在岗情况字典（在岗/内退）
    this.getEmploymentForm() // 用工形式字典
    this.getEmploymentDuration() // 用工期限字典
    this.getReductionManWays() // 减员途径字典
    this.getRegisterResidenceNature() // 户口性质字典
    // this.getList()
  },
  methods: {
    /*
     * 新增
     */
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.ruleForm))
          if (data.professionalCategory) {
            if (data.professionalCategory.constructor === Array) {
              data.professionalCategory = data.professionalCategory.pop()
            }
          }
          if (data.addManWays) {
            if (data.addManWays.constructor === Array) {
              data.addManWays = data.addManWays.pop()
            }
          }
          data.baseCode = this.baseCode
          data.sysOrgCode = '测试部门'
          data.createUser = '测试员'
          postAction('/opBaseStaffSubset/add', data)
            .then(result => {
              if (result.code === 200) {
                this.dialogFormVisible = false
                bus.$emit('handleDialog')
                this.getList()
                this.$message({
                  type: 'success',
                  message: '新增成功!'
                })
              } else {
                this.$message({
                  type: 'waring',
                  message: result.msg
                })
              }
            })
        } else {
          return false
        }
      })
    },
    batchAdd(data){
      this.dialogFormVisible = true
      this.ruleForm.uuidList = data
    },
    handleCurrentChange(val) {
        this.currentRow = val;
    },

    /**
     * 删除
     */
    deleteRow (row) {
      if(!this.currentRow){
        this.$message("请选择要删除的数据！")
        return
      }

      this.$confirm('是否要删除这条数据 [姓名：'+this.currentRow.nameUsedBefore+ ']?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          postAction('/opBaseStaffSubset/delete', { uuids: [this.currentRow.uuid] })
            .then((res) => {
              if (res.code === 200) {
                this.getList()
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
              } else {
                this.$message({
                  type: 'waring',
                  message: res.msg
                })
              }
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    /**
     * 修改
     */
    updateForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.ruleForm))
          if (data.professionalCategory) {
            if (data.professionalCategory.constructor === Array) {
              data.professionalCategory = data.professionalCategory.pop()
            }
          }
          if (data.addManWays) {
            if (data.addManWays.constructor === Array) {
              data.addManWays = data.addManWays.pop()
            }
          }
          data.updateUser = '修改员'
          postAction('/opBaseStaffSubset/update', data)
            .then(result => {
              if (result.code === 200) {
                this.dialogFormVisible = false
                bus.$emit('handleDialog')
                this.getList()
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
              } else {
                this.$message({
                  type: 'waring',
                  message: result.msg
                })
              }
            })
        } else {
          return false
        }
      })
    },
    handleOffDialog() {
      this.dialogFormVisible = false
      bus.$emit('handleDialog')
    },
    /**
     * 查询
     */
    getList () {

      let self = this
      postAction('/opBaseStaffSubset/opBaseStaffSubsetSearch', { baseCode: this.baseCode,size:this.pagination.pageSize, page:this.pagination.pageNum})
        .then(result => {
          if (result.code === 200) {
            if (!result.data.list.length && self.pagination.pageNum !== 1) {
              self.pagination.pageNum -= 1
              self.getList()
              return
            }
            if (self.pagination.pageNum === 1 && result.data.list.length === 0) {
              self.pagination.total = 0
            } else {
              self.pagination.total = result.data.total
            }
            self.tableData = result.data.list
          }
        })
    },

    /**
     * 获取专业类别表
     */
    getSubjectList () {
      getAction('/opDictNew/findTreeByType?type=rs_ai')
        .then((res) => {
          if (res.code === 200) {
            this.subjectList = res.data
          }
        })
    },

    /**
     * 获取增员途径表
     */
    getWaysList () {
      getAction('/opDictNew/findTreeByType?type=reduction_man_add')
        .then(result => {
          if (result.code === 200) {
            this.waysList = result.data
          }
        })
    },

    /**
     * 获取在岗情况字典（在岗/内退）
     */
    async getJobCondition () {
      const result = await getAction(opDictNew.findByType, {
        type: 'on_job_condition'
      })
      this.anJobConditionList = result.data
    },

    /**
     * 用工形式字典
     */
    async getEmploymentForm () {
      const result = await getAction(opDictNew.findByType, {
        type: 'employment_form'
      })
      this.employmentFormList = result.data
    },

    /**
     * 用工期限字典
     */
    async getEmploymentDuration () {
      const result = await getAction(opDictNew.findByType, {
        type: 'employment_duration'
      })
      this.employmentDurationList = result.data
    },

    /**
     * 减员途径字典
     */
    async getReductionManWays () {
      const result = await getAction('/opDictNew/findTreeByType', {
        type: 'reduction_man_ways'
      })
      this.reductionManWaysList = result.data
    },

    /**
     * 户口性质字典
     */
    async getRegisterResidenceNature () {
      const result = await getAction(opDictNew.findByType, {
        type: 'registered_residence_nature'
      })
      this.registerResidenceNatureList = result.data
    },

    /**
     * 详情
     */
    detailRow (row) {
      if(!row){
        this.$message("请选择要编辑的数据！")
        return
      }
      this.ruleForm = JSON.parse(JSON.stringify(row))
      this.isUpdate = true
      this.dialogFormVisible = true
    },

    rowClass ({ row, rowIndex }) {
      return 'background:rgba(248, 248, 249, 1);color:#000;height:30px;'
    },
    handleChange () {},
    handleClose () {}
  }
}
</script>

<style >
.inputW {
  width: 100%;
}
::v-deep .el-dialog__body {
  padding: 30px 20px 0;
}
.table_tools {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
    margin-bottom: 2px;
    margin-top: 2px;
}
.el-form-item__label {
  font-weight: bold;
}
.el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 100%;
}
.el-dialog {
  width: 850px;
}
.vxe-table--render-default.size--mini .vxe-body--column.col--ellipsis, .vxe-table--render-default.size--mini .vxe-footer--column.col--ellipsis, .vxe-table--render-default.size--mini .vxe-header--column.col--ellipsis, .vxe-table--render-default.vxe-editable.size--mini .vxe-body--column {
    height: 30px;
}

.el-table .cell {
    line-height: 20px;
}

.el-tabs__content {
    overflow: auto;
    position: relative;
}
</style>
